/*
 * @lc app=leetcode.cn id=242 lang=swift
 *
 * [242] 有效的字母异位词
 */

// @lc code=start
class Solution242 {
    // 将数组的下标当做hash表实现
    // 第二中解法(比第一种解法稍微优势点，但没太大区别)
    // 当元素较少时。
    func isAnagram(_ s: String, _ t: String) -> Bool {
        var record:[Int] = Array(repeating: 0, count: 26)
        for item in Array(s) {
            let hashIndex:Int = Int(item.asciiValue! - Character("a").asciiValue!)
            record[hashIndex] += 1
        }
        for item in Array(t) {
            let hashIndex:Int = Int(item.asciiValue! - Character("a").asciiValue!)
            record[hashIndex] -= 1
        }
        for item in record {
            if item != 0 {
                return false
            }
        }
        return true
    }

    static func test() {
        let solu = Solution242()
        let flag = solu.isAnagram("rat", "car")
        print(flag)
    }
}
// @lc code=end

